package com.example.indenture.company.controller;

import com.example.indenture.common.vo.Result;
import com.example.indenture.company.entity.Article;
import com.example.indenture.company.service.ArticleService;
import com.example.indenture.race.entity.Race;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: wyt
 * @Date: 2023/6/10 18:11
 **/
@RestController
@RequestMapping("/api")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    // 添加公告
    @RequestMapping("/article/add")
    public Result<Object> addArticle(@RequestBody Article article) {
        System.out.println("Adding article: " + article); // 输出文章信息，检查是否正确传入
        return articleService.addArticle(article)==1? Result.ok(article):Result.failed("添加失败");
    }

    // 删除公告
    @RequestMapping("/article/deleteByCidAndRid/{cid}/{rid}")
    public Result<Object> deleteArticleByCidAndRid(@PathVariable Integer cid, @PathVariable Integer rid) {
        if (cid == null || rid == null) {
            return Result.failed("Invalid cid or rid");
        }
        articleService.deleteArticleByCidAndRid(cid, rid);
        return Result.ok("Article deleted successfully");
    }


    @RequestMapping("/article/getByCidAndRid/{cid}/{rid}")
    public Result<List<Article>> getArticleByCidAndRid(@PathVariable Integer cid, @PathVariable Integer rid) {
        if (cid == null || rid == null) {
            return Result.failed("Get failed by yourself");
        }
        List<Article> articles = articleService.getArticleByCidAndRid(cid, rid);
        return articles != null ? Result.ok(articles) : Result.failed("Get failed by service");
    }
}